package com.aaa.service.impl;

import com.aaa.entity.Role;
import com.aaa.mapper.RoleMapper;
import com.aaa.service.RoleService;
import com.aaa.vo.Result;
import com.aaa.vo.RoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: qy158-system
 * @description:
 * @author: 闫克起2
 * @create: 2022-10-11 10:34
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public Result findByUserId(Integer userId) {
        //1.查询所有的角色
        List<Role> list= roleMapper.selectAll();
        //2.查询用户具有的角色id
        List<String> roleIds= roleMapper.findRoleIdByUserId(userId);

        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("roleIds",roleIds);
        return new Result(2000,"查询成功",map);
    }

    @Override
    public Result qr(Integer userId, Integer[] roleIds) {
        //1.根据用户删除该用户具有的角色id
        roleMapper.deleteByUserId(userId);
        //2.添加新的角色id
        roleMapper.insertRoleId(userId,roleIds);
        return new Result(2000,"添加成功");
    }

    @Override
    public Result findByCondition(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Role> roles=roleMapper.selectByCondition(roleVo.getRoleName(),roleVo.getRoleDesc());
        PageInfo<Role> pageInfo=new PageInfo<>(roles);
        return new Result(2000,"查询成功",pageInfo);
    }
}
